package org.example.admin.service;

import org.example.admin.dto.AdGoodsDto;
import org.example.db.entity.Ad;
import org.example.db.entity.Goods;
import org.example.db.mapper.AdMapper;
import org.example.db.mapper.GoodsMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminAdService {
    AdMapper adMapper;
    GoodsMapper goodsMapper;

    public AdminAdService(AdMapper adMapper, GoodsMapper goodsMapper) {
        this.adMapper = adMapper;
        this.goodsMapper = goodsMapper;
    }

    public List<Ad> getAdList(){
        return adMapper.selectList(null);
    }

    public List<AdGoodsDto> getAdGoodsList(){

        List<Ad> ads = adMapper.selectList(null);
        ArrayList<AdGoodsDto> adGoodsDtoArrayList = new ArrayList<>();
        for(Ad ad: ads){
            Goods goods = goodsMapper.selectById(ad.getGoodsId());
            adGoodsDtoArrayList.add(new AdGoodsDto(ad.getId(), ad.getUrl(), ad.getName(), goods.getName(), goods.getPictureUrl()));
        }
        return adGoodsDtoArrayList;
    }

    public void updateAd(Ad ad){
        adMapper.updateById(ad);
    }

    public void deleteAd(Ad ad){
        adMapper.deleteById(ad.getId());
    }

    public void add(Ad ad){adMapper.insert(ad);}
}
